<template>
  <div class="article-list-container">
    <div class="content-wrapper">
      <h1 class="page-title">最新文章</h1>
      <div class="article-list" v-infinite-scroll="loadMore" :infinite-scroll-disabled="loading" infinite-scroll-distance="50">
        <div v-for="article in articles" :key="article.id" class="article-card animate__animated animate__fadeIn">
          <div class="article-content">
            <h2 class="article-title" @click="$router.push(`/home/article/${article.id}`)">{{ article.title }}</h2>
            <p class="article-description">{{ article.description }}</p>
            
            <div class="article-meta">
              <span class="time">
                <t-icon name="time" class="icon-time" />
                {{ formatDate(article.createdAt) }}
              </span>
              
              <div class="interaction-stats">
                <span class="stat">
                  <t-icon name="thumb-up" class="icon-like" />
                  {{ article.likeCount }}
                </span>
                <span class="stat">
                  <t-icon name="star" class="icon-star" />
                  {{ article.favoriteCount }}
                </span>
                <span class="stat">
                  <t-icon name="chat" class="icon-comment" />
                  {{ article.commentCount }}
                </span>
              </div>
            </div>
          </div>
        </div>
        
        <div v-if="loading" class="loading-more">
          <el-spinner size="medium" />
          <span>正在加载更多内容...</span>
        </div>
        
        <div v-if="noMore" class="no-more">
          <div class="divider">
            <span>已经到底啦 ~</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { getRecentArticles } from '@/api/articleApi'
import 'animate.css'

const articles = ref([])
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
const loading = ref(false)
const noMore = ref(false)

const fetchArticles = async () => {
  if (loading.value || noMore.value) return
  
  loading.value = true
  try {
    const response = await getRecentArticles(currentPage.value, pageSize.value)
    const newArticles = response.data
    
    if (newArticles.length === 0) {
      noMore.value = true
    } else {
      articles.value = [...articles.value, ...newArticles]
      total.value = response.data.total
    }
  } catch (error) {
    console.error('Failed to fetch articles:', error)
  } finally {
    loading.value = false
  }
}

const loadMore = () => {
  if (!loading.value && !noMore.value) {
    currentPage.value++
    fetchArticles()
  }
}

const formatDate = (dateString) => {
  const date = new Date(dateString)
  const now = new Date()
  const diff = now - date
  
  // 如果是当前分钟内
  if (diff < 60 * 1000) {
    const seconds = Math.floor(diff / 1000)
    return `${seconds}秒前`
  }
  
  // 如果是当前小时内
  if (diff < 60 * 60 * 1000) {
    const minutes = Math.floor(diff / (60 * 1000))
    return `${minutes}分钟前`
  }
  
  // 如果是当天
  if (diff < 24 * 60 * 60 * 1000) {
    const hours = Math.floor(diff / (60 * 60 * 1000))
    return `${hours}小时前`
  }
  
  // 如果是今年
  if (date.getFullYear() === now.getFullYear()) {
    return `${date.getMonth() + 1}月${date.getDate()}日`
  }
  
  // 如果是往年
  return `${date.getFullYear()}年${date.getMonth() + 1}月${date.getDate()}日`
}

onMounted(() => {
  fetchArticles()
})
</script>

<style scoped>
.article-list-container {
  padding: 40px 0;
  min-height: calc(100vh - 100px);
  background-color: #f8f9fa;
  width: 100%;
}

.content-wrapper {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.page-title {
  font-size: 2.5rem;
  font-weight: 700;
  color: #1a202c;
  margin-bottom: 40px;
  text-align: center;
  position: relative;
}

.page-title::after {
  content: '';
  position: absolute;
  bottom: -10px;
  left: 50%;
  transform: translateX(-50%);
  width: 60px;
  height: 4px;
  background: linear-gradient(90deg, #007BFF, #00c6ff);
  border-radius: 2px;
}

.article-list {
  display: flex;
  flex-direction: column;
  gap: 30px;
  margin-bottom: 40px;
}

.article-card {
  background: white;
  border-radius: 16px;
  padding: 30px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.05);
  transition: all 0.4s cubic-bezier(0.165, 0.84, 0.44, 1);
  border: 1px solid rgba(0, 0, 0, 0.03);
  overflow: hidden;
  position: relative;
}

.article-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 4px;
  height: 100%;
  background: linear-gradient(180deg, #007BFF, #00c6ff);
  opacity: 0;
  transition: opacity 0.3s ease;
}

.article-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
}

.article-card:hover::before {
  opacity: 1;
}

.article-content {
  padding-left: 10px;
}

.article-title {
  font-size: 1.8rem;
  color: #1a202c;
  margin-bottom: 16px;
  font-weight: 700;
  cursor: pointer;
  transition: color 0.3s ease;
  line-height: 1.3;
}

.article-title:hover {
  color: #007BFF;
}

.article-description {
  color: #4a5568;
  margin-bottom: 24px;
  line-height: 1.8;
  font-size: 1.1rem;
  opacity: 0.9;
}

.article-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #718096;
  font-size: 0.95rem;
  padding-top: 16px;
  border-top: 1px solid #edf2f7;
}

.time {
  display: flex;
  align-items: center;
  gap: 8px;
}

.icon-time {
  color: #718096;
}

.interaction-stats {
  display: flex;
  gap: 24px;
}

.stat {
  display: flex;
  align-items: center;
  gap: 8px;
  transition: transform 0.2s ease, color 0.2s ease;
}

.stat:hover {
  transform: translateY(-2px);
}

.icon-like:hover {
  color: #e53e3e;
}

.icon-star:hover {
  color: #ecc94b;
}

.icon-comment:hover {
  color: #38b2ac;
}

.loading-more {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 12px;
  padding: 30px 0;
  color: #718096;
  font-size: 1rem;
}

.no-more {
  padding: 40px 0;
}

.divider {
  display: flex;
  align-items: center;
  text-align: center;
  color: #a0aec0;
  font-size: 1rem;
}

.divider::before,
.divider::after {
  content: '';
  flex: 1;
  border-bottom: 1px solid #e2e8f0;
}

.divider span {
  padding: 0 24px;
  font-style: italic;
}
</style>
